Amazon Simple E-mail Service (SES for now), it is a Sending E-mail Service provided by amazon, with a real low cost for users. The plugin interacts with it, so you can send e-mails using SES infrastructure without extra code in your app.If you just want to send e-mails using your non-SES infrastructure, this plugin won't help you, I recommend you to use the great Grails Mail Plugin (http://grails.org/plugin/mail).Unfortunately, SES does not provide a simple SMTP server address to connect and use, otherwise we could just configure the Grails Mail Plugin to use it. Instead of this, AWS provides a http web service for it.But, if you really want to use Mail Plugin, you can configure AWS SES with a SMTP Bridge to connect to your SMTP server, but only if you're running Postfix or Sendmail locally. For doing this, check AWS SES Developer Guide.
You have to be subscribed to SES, if you are not yet, do it before using the plugin.
.h3 Interacting with SESGrails AWS Plugin adds two types of interacting with AWS Simple Email Service.Introductory info on Gant Scripts for AWS Plugin is described in the beginning of this guide (section 1.4).To let AWS SES send e-mails using your e-mail as 'from' address, you have to authorize it.To to so, you can run the script:grails aws-ses-verify-email
You'll be prompted about the e-mail address to verify. After that, SWS will send an confirmation e-mail to this address, you should click on the link in that e-mail to start sending emails using this from.You will have to do it for all emails you want to use with SES.This will allow you to see all verified emails with amazon with the configured credentials.To use:grails aws-ses-list-verified-emails
Retrieves for you the current quota for you. Will give you information on how much emails you can send per day, per second, and the number of e-mails you're allowed to send.grails aws-ses-get-send-quota
(from aws docs) Returns the user's sending statistics (the amount of sent mails, bounced, spammed and etc). The output is a list of items, for the last two weeks of sending activity. Each item in the list contains statistics for a 15-minute interval.grails aws-ses-get-send-statistics
Attention: If you only subscribed to Amazon SES and still didn't get access for production using, you'll only be allowed to send e-mails 'from' AND 'to' emais that has been verified with amazon. So, you'll have to use the 'grails aws-ses-verify-email' for both sender and recipient adresses. After getting production access, this won't be needed.
This script is a simple utility to test if amazon has verified your email correctly.It will queue one test email message for SES. You'll be prompted for the FROM email address and the TO email address to send. Remember, you have to verify these emails first.grails aws-ses-send-ping-mail
Attention: If you only subscribed to Amazon SES and still didn't get access for production using, you'll only be allowed to send e-mails 'from' AND 'to' emais that has been verified with amazon. So, you'll have to use the 'grails aws-ses-verify-email' for both sender and recipient adresses. After getting production access, this won't be needed.
Sending e-mails from your application is easy, and you can do it from your services or controllers just calling the sesMail closure that is injected on these artifacts.class SesTestController { def sendPlainTextMail = { def mailId = sesMail {
to "test-email@gmail.com"
subject "test plain text mail"
body "sendPlainTextMail (${new Date().format('dd/MM/yyyy HH:mm')})"
} render "E-mail sent: ${mailId}"
}
}
The only option you may want to set on your Config.groovy will be the default from e-mail address that will be used in your e-mails. As said before, this address have to be verified with AWS (check Gant scripts section to see how to do it).To configure it, you'll do the following inside the grails.plugin.aws section of your application Config.grails {
plugin {
aws { ses {
from = "my-verified-email@gmail.com"
} }
}
}
Of course, you can override this parameter in every call to the plugin, setting the 'from' parameter.To send e-mails, just call the sesMail closure as seen beforedef mailId = sesMail {
from "origin@gmail.com"
to "test-email@gmail.com"
subject "test plain text mail"
body "this is the e-mail content, sent at: (${new Date().format('dd/MM/yyyy HH:mm')})"
}
The sesMail returns an unique identifier for this e-mail at Amazon infrastructure, as this one:0000012dcde0ea1c-d331d2ec-3972-4a6c-825f-fc980cde3352-000000
Amazon don't provide anything useful to do with this id yet, but I imagine that in a near future, you'll be able to retrieve if this email was delivered correctly, or if it was spammed or bounced. So, feel free to store it if you want to.Closure parameters
AWS Credentials
As the S3 file upload support, you can override AWS credentials settings for sending mails. This is rare and unusual, but if you really need, do the same as in S3.credentials "new-access-key", "new-secret-key"
Just remember, if this new AWS credentials belong to another AWS account, this account will have to be subscribed to SES service and have verified e-mails to send.Defining the sender
As seen in the other topic, you can set this default address in the application Config, but if you want to explicitly set the address in each email you send, just override the from method:from "my-other-email@gmail.com"
Defining recipient e-mails
To
To set the recipient e-mail, use the to method.You can send to multiple addresses doing this:to "usermyapp.com"
, "user2myapp.com", "user3@myapp.com"
CC and BCC
To set CC and BCC addresses, you can do the same as to addresses:bcc "you-dont-know-me@myapp.com"
And again, for multiple addresses:ccc "user1@myapp.com", "user2@myapp.com", "user3@myapp.com"
bcc "user4@myapp.com", "user5@myapp.com", "user6@myapp.com"
Setting the e-mail subject
To set the subject, following other parameters convention:subject "Testing the AWS Plugin"
E-mail body
And finally, the e-mail body can be set with:body "This is the text user will receive in the e-mail body"
You can send html emails the same way you'd do with text e-mails, just instead of calling the body method, you have to pass the content to the html method:sesMail {
to "email@gmailcom"
subject "testing html emails"
html "<html><body><h3>HTML email</h3><strong>Strong text</strong></body></html>"
}
Using GSP templates to generate html emails
I bet you don't want to write all the HTML in your closure, so, you can design your e-mail in a grails template and use it to define the email content:sesMail {
to "email@gmailcom"
subject "testing html emails"
html g.render(template: "/email-templates/template", model: [name: "Lucas", now: new Date()])
}
This will render the /email-templates/_template.gsp passing the model map to the rendering engine. For example, let's imagine this file has the following content:<html>
<body>
<h2>Testing e-mail templates</h2>
<ul>
<li>Name: ${name}</li>
<li>Now: ${now.format('dd/MM/yyyy HH:mm')}</li>
</ul>
</body>
</html>
This will send the html e-mail with the content below rendered:<html>
<body>
<h2>Testing e-mail templates</h2>
<ul>
<li>Name: Lucas</li>
<li>Now: 28/01/2010 18:02</li>
</ul>
</body>
</html>
You can use both methods html and body to send e-mails with both content in it. This is usually the best approach, since user may not allowed his email client to render html emails. So, it is up to you.